#!/bin/sh

####inotify脚本   实时同步Path目录到同步服务器

####Path         本地需要同步的目录
####yuanchengIp  远程同步服务器IP
####localIP      本地IP
####Rsyncuser    rsync远程用户名
####Rsyncmoudle  rsync远程配置模块名


Path=/data

yuanchengIp=172.16.1.41

localIP=$(ifconfig |grep Bcast|sed -r 's#(^.*addr:)(.*)(  Bcast.*$)#\2#g'|xargs|sed 's# #---#g')

Rsyncuser=rsync_backup

Rsyncmoudle=nfsbackup/



/usr/bin/inotifywait -mrq --format '%w%f' -e close_write,delete $Path \
|while read file
 do
 if [ -f $file  ];then
#$rule=`ls $file|sed -r 's#(/data)(.*/)(.*$)#\1\2#g'`
#$mulu=`pwd $file`
#ls$mulu
#exit
     rsync -azR $file --delete $Rsyncuser@$yuanchengIp::$Rsyncmoudle$localIP --password-file=/etc/rsyncpw.pw &
#$mulu=`echo $file|sed -r 's#(/data)(.*/)(.*$)#\1\2#g'`
#mulu=`ls $file|sed -r 's#(/data)(.*/)(.*$)#\2#g'`
#rsync -az $file --delete $Rsyncuser@$Ip::$Rsyncmoudle$mulu --password-file=/etc/rsyncpw.pw
 else
     cd $Path
     rsync -azR ./  --delete $Rsyncuser@$yuanchengIp::$Rsyncmoudle$localIP$Path --password-file=/etc/rsyncpw.pw &
 fi
 done